package com.citywithincity.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.citywithincity.db.Record;


public class MemoryUtil {
	/**
	 * 清空array
	 * @param array
	 */
	public static <T> void clearArray(T[] array){
		for(int i=0 , count = array.length; i < count; ++i){
			array[i] = null;
		}
	}
	public static void clearIntArray(int[] indexes) {
		for (int i=0 , count = indexes.length; i < count; ++i) {
			indexes[i] = -1;
		}
	}
	
	public static Map<String, Object> toKeyValue(List<Record> list,String key,String value){
		Map<String, Object> result = new HashMap<String, Object>();
		for (Record record : list) {
			result.put(record.getString(key), record.get(value));
		}
		return result;
	}
	

	/**
	 * 合并
	 * 将src中的map合并至dest
	 */
	public static void mergeMap(Map<String, Object> dest,Map<String, Object> src) {
		dest.putAll(src);
	}
	
	/**
	 * map 转为string
	 * @param map
	 * @return
	 */
	public static String mapToString(Map<String, ?> map){
		boolean first = true;
		StringBuilder buffer = new StringBuilder("{");
		for (Entry<String, ?> entry : map.entrySet()) {
			if(first){
				first = false;
			}else{
				buffer.append(',');
			}
			buffer.append(entry.getKey())
			.append(':')
			.append(entry.getValue());
		}
		buffer.append("}\n");
		return buffer.toString();
	}
	
	
	
	
	/**
	 * map 转为 xml
	 * 
	 * @param map
	 * @return
	 */
	public static String mapToXml(Map<String, Object> map) {
		StringBuilder sb = new StringBuilder("<xml>");
		for (Entry<String, Object> entry : map.entrySet()) {
			sb.append('<').append(entry.getKey()).append('>')
					.append(entry.getValue()).append("</")
					.append(entry.getKey()).append('>');
		}
		sb.append("</xml>");
		return sb.toString();
	}


	

	
	
}
